using Microsoft.VisualBasic;
using Microsoft.VisualBasic.CompilerServices;
using SJVars;
using SJXmlP.XMLClasses;
using System;
using System.Collections;
using System.Reflection;
using System.Text;

namespace yW5Lib
{
	public class clsWordLogs : IEnumerable
	{
		private clsXml cXml;

		private clsCollection<clsWordLog> mvarCol;

		public clsCollection<clsWordLog> Col => mvarCol;

		public clsWordLogs()
		{
			cXml = new clsXml();
			mvarCol = new clsCollection<clsWordLog>();
		}

		public void Clear()
		{
			IEnumerator enumerator = default(IEnumerator);
			try
			{
				enumerator = mvarCol.GetEnumerator();
				while (enumerator.MoveNext())
				{
					((clsWordLog)enumerator.Current).Clear();
				}
			}
			finally
			{
				if (enumerator is IDisposable)
				{
					(enumerator as IDisposable).Dispose();
				}
			}
			mvarCol.Clear();
		}

		public bool CreateFromXML(string XMLData)
		{
			bool result = true;
			try
			{
				Clear();
				if (Strings.InStr(XMLData, "<WCLog>", CompareMethod.Text) != 0)
				{
					string[] DataArray = new string[2];
					cXml.ExtractXMLArray(XMLData, "<WCLog>", "</WCLog>", "</WC>", ref DataArray);
					int num = DataArray.Length - 1;
					for (int i = 0; i <= num; i++)
					{
						if (Strings.InStr(1, DataArray[i], "<WC>", CompareMethod.Text) > 0)
						{
							clsWordLog clsWordLog = new clsWordLog();
							clsWordLog.CreateFromXML(DataArray[i]);
							mvarCol.Add(clsWordLog, modDateTime.DateToStringNoTime(clsWordLog.LogDate));
						}
					}
					DataArray = null;
					return result;
				}
				return result;
			}
			catch (Exception ex)
			{
				ProjectData.SetProjectError(ex);
				Exception ex2 = ex;
				clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
				result = false;
				ProjectData.ClearProjectError();
				return result;
			}
		}

		public bool WriteXML(ref StringBuilder SBP)
		{
			bool result = true;
			try
			{
				SBP.AppendLine("\t<WCLog>");
				IEnumerator enumerator = default(IEnumerator);
				try
				{
					enumerator = mvarCol.GetEnumerator();
					while (enumerator.MoveNext())
					{
						clsWordLog clsWordLog = (clsWordLog)enumerator.Current;
						SBP.AppendLine(clsWordLog.DataAsXML());
					}
				}
				finally
				{
					if (enumerator is IDisposable)
					{
						(enumerator as IDisposable).Dispose();
					}
				}
				SBP.AppendLine("\t</WCLog>");
				return result;
			}
			catch (Exception ex)
			{
				ProjectData.SetProjectError(ex);
				Exception ex2 = ex;
				result = false;
				clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
				ProjectData.ClearProjectError();
				return result;
			}
		}

		public void UpdateCount(int WordCountExclUnused, int WordCountWithUnused)
		{
			UpdateCount(WordCountExclUnused, WordCountWithUnused, DateTime.Now.Date);
		}

		public void UpdateCount(int WordCountExclUnused, int WordCountWithUnused, DateTime CountDate)
		{
			string key = modDateTime.DateToStringNoTime(CountDate);
			clsWordLog clsWordLog = new clsWordLog();
			try
			{
				if (mvarCol.Contains(key))
				{
					mvarCol.Remove(key);
				}
				clsWordLog.LogDate = CountDate;
				clsWordLog.WordCount = WordCountExclUnused;
				clsWordLog.TotalWordCount = WordCountWithUnused;
				mvarCol.Add(clsWordLog, key);
			}
			catch (Exception ex)
			{
				ProjectData.SetProjectError(ex);
				Exception ex2 = ex;
				clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
				ProjectData.ClearProjectError();
			}
		}

		public int CountonDate(DateTime LogDate, bool WithUnused)
		{
			string key = modDateTime.DateToStringNoTime(LogDate.Date);
			if (mvarCol.Contains(key))
			{
				clsWordLog clsWordLog = mvarCol[key];
				if (WithUnused)
				{
					return clsWordLog.TotalWordCount;
				}
				return clsWordLog.WordCount;
			}
			return 0;
		}

		public int LatestDiff(bool WithUnused)
		{
			int result = default(int);
			try
			{
				if (mvarCol.Count() != 0)
				{
					clsWordLog clsWordLog;
					if (mvarCol.Count() != 1)
					{
						clsWordLog = mvarCol[mvarCol.Count()];
						clsWordLog clsWordLog2 = mvarCol[mvarCol.Count() - 1];
						result = ((!WithUnused) ? (clsWordLog.WordCount - clsWordLog2.WordCount) : (clsWordLog.TotalWordCount - clsWordLog2.TotalWordCount));
						if (DateTime.Compare(clsWordLog.LogDate, DateTime.Now.Date) >= 0)
						{
							return result;
						}
						result = 0;
						return result;
					}
					clsWordLog = mvarCol[1];
					result = ((!WithUnused) ? clsWordLog.WordCount : clsWordLog.TotalWordCount);
					if (DateTime.Compare(clsWordLog.LogDate, DateTime.Now.Date) >= 0)
					{
						return result;
					}
					result = 0;
					return result;
				}
				result = 0;
				return result;
			}
			catch (Exception ex)
			{
				ProjectData.SetProjectError(ex);
				Exception ex2 = ex;
				clsShared.cLog.AddAction(ex2, MethodBase.GetCurrentMethod());
				ProjectData.ClearProjectError();
				return result;
			}
		}

		public IEnumerator GetEnumerator()
		{
			return mvarCol.GetEnumerator();
		}

		IEnumerator IEnumerable.GetEnumerator()
		{
			//ILSpy generated this explicit interface implementation from .override directive in GetEnumerator
			return this.GetEnumerator();
		}
	}
}
